package com.appheader.rongmaphone.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.appheader.rongmaphone.R;
import com.appheader.rongmaphone.cache.preference.PreferenceUtil;
import com.appheader.rongmaphone.constants.GlobalVars;
import com.appheader.rongmaphone.utils.network.FileUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TestActivity extends AppCompatActivity implements View.OnClickListener {


    private final static String PATH = "http://img.armyhorse.com/public_resource/files/image/FIL886BDE2F-DCCA-4ADD-928F-5646A56B6169.png@!base_style";
    private final static String imagePath = "data/data/" + GlobalVars.getPackageInfo().packageName + File.separator + "image" + File.separator + "flower.png";
    private final static String sdcardRoot = Environment.getExternalStorageDirectory()
            + File.separator
            + GlobalVars.getPackageInfo().packageName
            + "/download"
            + File.separator + "public_resource/files/image/";
    private Button button, button2, readLocalImg;
    private ImageView localImg;
    private PreferenceUtil preferenceUtil;
    static final String mimeType = "text/html";
    static final String encoding = "utf-8";
    private final static String mSdRootPath = Environment.getExternalStorageDirectory() + File.separator + FileUtils.IMAGE_PATH + File.separator;
    //    private final static String mSdRootPath = "/mnt/shell/emulated/0" + File.separator+FileUtils.IMAGE_PATH+File.separator;
    private String uid;
    private WebView webView;
    private String path1 = "file:///storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc734ce182da5f183c90a3&pageId=57fcd3abe182da676170040e&pageType=phone&isClient=true";

    private String path2 = "file:///storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc719fe182da4aa10a88bc&pageId=580242c3e182da753ede6f27&pageType=phone&isClient=true";
    private String path3 = "file:///storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc719fe182da4aa10a88bc&pageId=580242c7e182da753ede6f28&pageType=phone&isClient=true";
    private String path4="file:///storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc719fe182da4aa10a88bc&pageId=57fc719fe182da4aa10a88bd&pageType=phone&isClient=true";
    private String path5="file:///storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc719fe182da4aa10a88bc&pageId=580242bce182da753ede6f25&pageType=phone&isClient=true";
    private ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        button = (Button) findViewById(R.id.downLoad);
        button2 = (Button) findViewById(R.id.downLoad2);
        webView = (WebView) findViewById(R.id.locla_webview);
        readLocalImg = (Button) findViewById(R.id.read_local_img);
        localImg = (ImageView) findViewById(R.id.local_img);
        img = (ImageView) findViewById(R.id.stopImg);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
//        加载需要显示的网页
        webSettings.setBlockNetworkImage(false);
        webSettings.setBlockNetworkLoads(false);
        webSettings.setDomStorageEnabled(true);

        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        readLocalImg.setOnClickListener(this);
        img.setOnClickListener(this);
        preferenceUtil = new PreferenceUtil(getApplicationContext());
    }

    private void setImage(String path) {
        localImg.setImageBitmap(FileUtils.getBitmap(path));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.downLoad:
                break;
            case R.id.downLoad2:
//              webView.loadDataWithBaseURL(null,"file://storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc734ce182da5f183c90a3&pageId=57fcd3b3e182da6761700410&pageType=phone&isClient=true",mimeType,encoding,null);
                WebSettings settings = webView.getSettings();
                settings.setAllowFileAccessFromFileURLs(true);
                settings.setJavaScriptEnabled(true);
//                webView.loadUrl("file://storage/emulated/0/com.appheader.rongmaphone/download/57f9a48ae182da4aa10a87f3/web/preview/index.html?projectId=57fc734ce182da5f183c90a3&pageId=57fcd3b3e182da6761700410&pageType=phone&isClient=true");
                webView.loadUrl(path3);
                break;
            case R.id.read_local_img:
//                setImage();//不可行
//                String path = FileUtils.getStorageDirectory() + File.separator + FileUtils.getImageName(PATH);
//                Log.e("TEST_TAG", "onClick: "+path);
//                Log.e("TEST_TAG", "onClick: "+mSdRootPath);
//                String imageName = FileUtils.getImageName(PATH);
//                Bitmap localBitmap = getLocalBitmap(imagePath);

                //这种不行
//                Bitmap localBitmap = BitmapFactory.decodeFile(sdcardRoot+FileUtils.getImageName(PATH));
//                localImg.setImageBitmap(localBitmap);
                //方法一、
                Log.e("TestActivity", "onClick: " + sdcardRoot + FileUtils.getImageName(PATH));
//                Bitmap localBitmap = getLocalBitmap(sdcardRoot+FileUtils.getImageName(PATH));
//                localImg.setImageBitmap(localBitmap);
                //方法二、
                FileInputStream f = null;
                try {
                    f = new FileInputStream(sdcardRoot + FileUtils.getImageName(PATH));
                    Bitmap bm = null;
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 8;//图片的长宽都是原来的1/8
                    BufferedInputStream bis = new BufferedInputStream(f);
                    bm = BitmapFactory.decodeStream(bis, null, options);
                    localImg.setImageBitmap(bm);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.stopImg:
                Toast.makeText(TestActivity.this,"点击了ImageView控件",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public Bitmap getLocalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
//            return BitmapFactory.decodeFile(mSdRootPath+name);
    }
}
